#include <stdio.h>
#include <stdlib.h>
#include "linklistfun.h"
/*
typedef struct linknode
{
    int data;
    struct linknode *next;
}link_node;
*/

link_node * creat_headnode(void)
{
	link_node *D = (link_node *)malloc(sizeof(link_node));
	D->data=-1;
    D->next=NULL;

    return D;
}

int headinsert(link_node *D)
{
	link_node *H=D;
	link_node *p = (link_node *)malloc(sizeof(link_node));
	printf("you will headinsert\n");
	printf("plz input data>>\n");
	scanf("%d",&(p->data));
	p->next=H->next;
	H->next=p;

	return 0;
}

int tailinsert(link_node *D)
{
	link_node *H=D;
	link_node *p = (link_node *)malloc(sizeof(link_node));
	printf("you will tailinsert\n");
	printf("plz input data>>\n");
	scanf("%d",&(p->data));
	while(H->next!=NULL)
	{
		H=H->next;
	}
	p->next=H->next;
	H->next=p;

	return 0;
}

int numberinsert(link_node *D)
{
	link_node *H=D;
	link_node *p = (link_node *)malloc(sizeof(link_node));
	printf("you will numberinsert\n");
	printf("plz input data>>\n");
	scanf("%d",&(p->data));
	printf("plz input number>>\n");
	int flag=0;
	scanf("%d",&flag);
	for(int i=0;i<flag;i++)
	{
		H=H->next;
	}
	p->next=H->next;
	H->next=p;

	return 0;
}

int headdelete(link_node *D)
{
	link_node *H=D;
	link_node *p = H->next;
	H->next=p->next;
	free(p);

	return 0;
}

int taildelete(link_node *D)
{
	link_node *H=D;

	while(H->next->next!=NULL)
	{
		H=H->next;		
	}
	link_node *p =H->next;
	free(p);
	H->next=NULL;

	return 0;
}

int numdelete(link_node *D)
{
	link_node *H=D;

	int flag=0;
	printf("plz input number>>\n");
	scanf("%d",&flag);
	for(int i=0;i<flag-1;i++)
	{
		H=H->next;
	}
	link_node *p=H->next;
	H->next=p->next;
	free(p);

	return 0;
}
int view(link_node *D)
{
	link_node *p=D->next;
	while(p!=NULL)
	{
		printf(">>>>%d\n",p->data);
		p=p->next;
	}

	return 0;
}
int backposdelete(link_node *D)
{
	int backpos;
	link_node *p=D;
	link_node *q=D;
	
	printf("plz input backpos>>>\n");
	scanf("%d",&backpos);
	for(int i=0;i<backpos;i++)
	{
		p=p->next;
	}
	while(p->next!=NULL)
	{
		p=p->next;
		q=q->next;
	}
	link_node *k=q->next;
	q->next=k->next;
	free(k);

	return 0;
}
